// https://leetcode.cn/problems/sort-array-by-parity/
// Created by ade on 2022/8/22.
//
#include <iostream>
#include <vector>
#include <algorithm>


using namespace std;

class Solution {
public:
    vector<int> sortArrayByParity1(vector<int> &nums) {
        int right = 1;
        int len = nums.size();
        for (int i = 0; i < len; i++) {
            if (nums[i] % 2 == 0) {
                if (i == right) right++;
                continue;
            } else {
                while (right < len && nums[right] % 2 == 1) {
                    right++;
                }
                if (right == len) break;
                swap(nums[i], nums[right]);
            }
        }
        return nums;
    }

    vector<int> sortArrayByParity(vector<int> &nums){
        partition(nums.begin(), nums.end(),[](int x){
            return x % 2 == 0;
        });
        return nums;
    }
};

int main() {
    vector<int> nums = {0, 2, 4, 1, 6};
    Solution so;
    auto a = so.sortArrayByParity(nums);
    for (auto i : a) {
        cout << i << ",";
    }
    cout << endl;
    return 0;
}